/*
 * @lc app=leetcode.cn id=128 lang=cpp
 *
 * [128] 最长连续序列
 */
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <string>
#include <cmath>

using namespace std;
// @lc code=start
class Solution {
public:
    int longestConsecutive(vector<int>& nums) {
        unordered_set<int> num_set;
        for(auto num: nums) {
            num_set.insert(num);
        }
        int ans = 0;
        for(auto num: num_set) {
            //value出现的次数 count() 返回匹配给定主键的元素的个数
            //从头开始，及没有前一个数的开始
            if (!num_set.count(num - 1)) {
                int currentNum = num;
                int currentStreak = 1;
                while (num_set.count(currentNum + 1)) {
                    currentNum += 1;
                    currentStreak += 1;
                }
                ans = max(ans, currentStreak);
            }
        }
        return ans;
    }
};
// @lc code=end

